#include "NotebookCtrl.h"

int ID_NOTEBOOK_PAGE_CLOSE = wxNewId();
int ID_NOTEBOOK_PAGE_CLOSE_ALL = wxNewId();
int ID_NOTEBOOK_PAGE_CLOSE_OTHERS = wxNewId();

BEGIN_EVENT_TABLE(NotebookCtrl,wxNotebook)

    EVT_MOUSE_EVENTS(NotebookCtrl::OnMouseEvent)

    EVT_MENU(ID_NOTEBOOK_PAGE_CLOSE, NotebookCtrl::OnPageClose)
	EVT_MENU(ID_NOTEBOOK_PAGE_CLOSE_ALL, NotebookCtrl::OnPageCloseAll)
	EVT_MENU(ID_NOTEBOOK_PAGE_CLOSE_OTHERS, NotebookCtrl::OnPageCloseOthers)

END_EVENT_TABLE()

IMPLEMENT_DYNAMIC_CLASS(NotebookCtrl,wxNotebook)

void NotebookCtrl::OnMouseEvent(wxMouseEvent& event)
{
    wxEventType eventType = event.GetEventType();
   
    if(dragTabID>=0)
    {
/*        if(eventType==wxEVT_LEFT_UP)
        {
            int id=HitTest(wxPoint(event.GetX(), event.GetY()));
            if(id>=0 && id!=dragTabID)
            {
                // move [dragTabID] to [id]
                DEBUG(wxString::Format(wxT("Drag %d->%d"), dragTabID, id));
                wxNotebookPage* page=GetPage(dragTabID);
                wxString text=GetPageText(dragTabID);
                int image=GetPageImage(dragTabID);
               
                RemovePage(dragTabID);
                InsertPage(id, page, text, true, image);
            }
           
            dragTabID=-1;
            wxSetCursor(wxCursor(wxCURSOR_ARROW));
        }
        else
        {
            wxCoord x=event.GetX();
            wxCoord y=event.GetY();
            int id=HitTest(wxPoint(x, y));
            if(id<0)
            {
                wxSetCursor(wxCursor(wxCURSOR_ARROW));
            }
            else if(x!=m_X || y!=m_Y)
            {
                wxSetCursor(wxCursor(wxCURSOR_HAND));
                m_X=m_Y=-1024;
            }
        }
*/
	}
    else // dragTabID==-1, no dragging
    {
        if(eventType==wxEVT_LEFT_DOWN)
        {
            //m_X=event.GetX();
            //m_Y=event.GetY();
            //dragTabID=HitTest(wxPoint(m_X, m_Y));
        }
        else if(eventType == wxEVT_RIGHT_DOWN)
        {
            rightClickedTabID = HitTest(event.GetPosition());
            
			wxMenu menu;
            menu.Append(ID_NOTEBOOK_PAGE_CLOSE, _("Close"));                     
			menu.Append(ID_NOTEBOOK_PAGE_CLOSE_ALL, _("Close All"));
			menu.Append(ID_NOTEBOOK_PAGE_CLOSE_OTHERS, _("Close Others"));

			if(this->GetPageCount() <= 1){
				menu.Enable(ID_NOTEBOOK_PAGE_CLOSE_OTHERS, false); 
			}
			
			PopupMenu(&menu);
		}else if(eventType == wxEVT_LEFT_DCLICK ){
			int id=HitTest(wxPoint(event.GetX(), event.GetY()));
			this->ClosePage(id);
		}
    }
   
    event.Skip();
}

void NotebookCtrl::OnPageClose(wxCommandEvent& event)
{
	this->ClosePage(rightClickedTabID);
}

void NotebookCtrl::OnPageCloseAll(wxCommandEvent& event)
{
	this->ClosePageAll();
}

void NotebookCtrl::OnPageCloseOthers(wxCommandEvent& event)
{
	this->ClosePageOthers(rightClickedTabID);
}

int NotebookCtrl::GetPageIndex(wxWindow* window)
{
	for(unsigned int i=0; i < GetPageCount(); i++){	
		if(window == GetPage(i))
			return i;
    }

	return -1;
}

void NotebookCtrl::ClosePage(int id)
{
	this->DeletePage(id);
}

void NotebookCtrl::ClosePageAll()
{
	this->DeleteAllPages();
}

void NotebookCtrl::ClosePageOthers(int id)
{
	int count = GetPageCount();
	for(unsigned int i=count; i>0; i--){	
		if(id != i-1){
			this->DeletePage(i-1);
		}
    }	
}
